package com.micro.ai.agents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.ai.commons.exception.BusinessException;
import com.micro.ai.agents.entity.AgentVersion;
import com.micro.ai.agents.mapper.AgentVersionMapper;
import com.micro.ai.agents.service.AgentVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 智能体版本服务实现
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@Service
public class AgentVersionServiceImpl extends ServiceImpl<AgentVersionMapper, AgentVersion> implements AgentVersionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentVersion create(AgentVersion version) {
        if (version == null) {
            throw new BusinessException("A0000", "版本信息不能为空");
        }
        
        if (!StringUtils.hasText(version.getAgentId())) {
            throw new BusinessException("A0000", "智能体ID不能为空");
        }
        
        if (!StringUtils.hasText(version.getVersion())) {
            throw new BusinessException("A0000", "版本号不能为空");
        }
        
        // 检查版本是否已存在
        LambdaQueryWrapper<AgentVersion> query = new LambdaQueryWrapper<>();
        query.eq(AgentVersion::getAgentId, version.getAgentId())
             .eq(AgentVersion::getVersion, version.getVersion());
        
        AgentVersion existing = this.getOne(query);
        if (existing != null) {
            throw new BusinessException("A0001", "版本号已存在: " + version.getVersion());
        }
        
        version.setCreatedAt(LocalDateTime.now());
        
        boolean success = this.save(version);
        if (!success) {
            throw new BusinessException("A0003", "保存版本失败");
        }
        
        log.info("创建智能体版本成功: versionId={}, agentId={}, version={}", 
                version.getId(), version.getAgentId(), version.getVersion());
        return version;
    }

    @Override
    public List<AgentVersion> listByAgentId(String agentId) {
        LambdaQueryWrapper<AgentVersion> query = new LambdaQueryWrapper<>();
        query.eq(AgentVersion::getAgentId, agentId)
             .orderByDesc(AgentVersion::getCreatedAt);
        
        return this.list(query);
    }

    @Override
    public Page<AgentVersion> pageQuery(int pageNum, int pageSize, String agentId) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 20;
        }
        
        Page<AgentVersion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AgentVersion> query = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(agentId)) {
            query.eq(AgentVersion::getAgentId, agentId);
        }
        
        query.orderByDesc(AgentVersion::getCreatedAt);
        
        return this.page(page, query);
    }
}

